"""
配置文件读取模块
"""

import configparser
import os


class Config:
    """配置管理类"""
    
    def __init__(self, config_file="config.ini"):
        """
        初始化配置
        
        Args:
            config_file: 配置文件路径
        """
        self.config = configparser.ConfigParser()
        self.config_file = config_file
        self.load_config()
    
    def load_config(self):
        """加载配置文件"""
        if os.path.exists(self.config_file):
            self.config.read(self.config_file, encoding='utf-8')
        else:
            # 如果配置文件不存在，使用默认值
            self.create_default_config()
    
    def create_default_config(self):
        """创建默认配置"""
        self.config['files'] = {
            'input_file': 'input/export-demo1.json',
            'template_file': 'template/template-word.docx',
            'output_dir': 'output'
        }
        
        self.config['document'] = {
            'include_timestamp': 'true',
            'add_page_break': 'false',
            'default_font_size': '12',
            'include_request_body_example': 'false',
            'include_response_example': 'false',
            'show_example_column': 'false'
        }
        
        self.config['format'] = {
            'section_start': '2',
            'format_json': 'true',
            'json_indent': '2'
        }
    
    def get(self, section, key, fallback=None):
        """
        获取配置值
        
        Args:
            section: 配置段
            key: 配置键
            fallback: 默认值
            
        Returns:
            配置值
        """
        return self.config.get(section, key, fallback=fallback)
    
    def getboolean(self, section, key, fallback=False):
        """
        获取布尔配置值
        
        Args:
            section: 配置段
            key: 配置键
            fallback: 默认值
            
        Returns:
            布尔值
        """
        return self.config.getboolean(section, key, fallback=fallback)
    
    def getint(self, section, key, fallback=0):
        """
        获取整数配置值
        
        Args:
            section: 配置段
            key: 配置键
            fallback: 默认值
            
        Returns:
            整数值
        """
        return self.config.getint(section, key, fallback=fallback)
    
    # 便捷方法
    @property
    def input_file(self):
        return self.get('files', 'input_file', 'input/export-demo1.json')
    
    @property
    def template_file(self):
        return self.get('files', 'template_file', 'template/template-word.docx')
    
    @property
    def output_dir(self):
        return self.get('files', 'output_dir', 'output')
    
    @property
    def include_timestamp(self):
        return self.getboolean('document', 'include_timestamp', True)
    
    @property
    def add_page_break(self):
        return self.getboolean('document', 'add_page_break', False)
    
    @property
    def default_font_size(self):
        return self.getint('document', 'default_font_size', 12)
    
    @property
    def section_start(self):
        return self.getint('format', 'section_start', 2)
    
    @property
    def format_json(self):
        return self.getboolean('format', 'format_json', True)
    
    @property
    def json_indent(self):
        return self.getint('format', 'json_indent', 2)
    
    @property
    def include_request_body_example(self):
        return self.getboolean('document', 'include_request_body_example', False)
    
    @property
    def include_response_example(self):
        return self.getboolean('document', 'include_response_example', False)
    
    @property
    def show_example_column(self):
        return self.getboolean('document', 'show_example_column', False)


if __name__ == "__main__":
    # 测试配置读取
    config = Config()
    print(f"输入文件: {config.input_file}")
    print(f"模板文件: {config.template_file}")
    print(f"输出目录: {config.output_dir}")
    print(f"包含时间戳: {config.include_timestamp}")
    print(f"默认字体大小: {config.default_font_size}")
    print(f"包含请求体示例: {config.include_request_body_example}")
    print(f"包含响应示例: {config.include_response_example}")
    print(f"显示示例列: {config.show_example_column}")
